//
//  DeliveryManagementCell.m
//  DingDongZP
//
//  Created by 杨政涛 on 2016/12/29.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "DeliveryManagementCell.h"
#import "DDCattleResume.h"

@implementation DeliveryManagementCell

static NSString *title = @"一键管理：开启状态下，简历打开，求职开始；关闭状态下，简历关闭，求职暂停，企业无法查看简历。";

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self queryCattleResumeState];
        
        [self setupView];
        
    }
    return self;
    
}
+(NSString *)title {
    return  title;
}

- (void) queryCattleResumeState
{
    
    DDCattleResume *cattleResume = [[DDCattleResume alloc]init];
    [cattleResume queryCattleResumeStateWithCattleUserID:[DDLoginUserManager userID] SuccessBlock:^(id responseObject) {
        
        if ([responseObject[@"data"][@"resume_show"] isEqualToString:@"1"]) {
            
            self.resume_show = YES;
        }else{
        
            self.resume_show = NO;
        }
        [self performSelectorOnMainThread:@selector(backToMainToUpdata) withObject:self waitUntilDone:YES];
    } failBlock:^(id responseObject, NSError *error) {
        
    }];
}

- (void)backToMainToUpdata
{
    
   self.switch0.on = self.resume_show;
}

- (void) setupView {

    
    self.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"投递管理";
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = COLOR_FONT_DARK_GRAY;
    [self addSubview:titleLabel];
    
    UILabel *conentLabel = [[UILabel alloc]init];
    conentLabel.numberOfLines = 0;
    conentLabel.textColor = COLOR_FONT_LIGHT_GRAY;
    
    

    NSMutableAttributedString *attr = [DDUtils attrWithString:6 font:[UIFont systemFontOfSize:13] text:@"一键管理：开启状态下，简历打开，求职开始；关闭状态下，简历关闭，求职暂停，企业无法查看简历。"];
    conentLabel.attributedText = attr;
    [self addSubview:conentLabel];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = COLOR_FONT_LITTLEGRAY;
    [self addSubview:lineView];
    
    
    self.switch0 = [[ZJSwitch alloc]initWithFrame:CGRectMake(0, 0, 25, 15)];
    self.switch0.transform = CGAffineTransformMakeScale(0.9,0.85);
    self.switch0.style = ZJSwitchStyleBorder;
    self.switch0.onTintColor = COLOR_FONT_ALERTGREEN;
    self.switch0.tintColor = COLOR_FONT_LITTLEGRAY;
    self.switch0.thumbTintColor = COLOR_WHITE;
    self.switch0.on = YES;
    [self.switch0 addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.switch0];
    
    
    [self.switch0 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-20);
        make.size.mas_equalTo(CGSizeMake(25, 15));
        make.centerY.mas_equalTo(titleLabel);
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(@(20));
        make.left.equalTo(@(20));
        make.height.equalTo(@(20));
    }];
    
    [conentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(titleLabel.mas_bottom).offset(15);
        make.left.equalTo(@(20));
        make.right.equalTo(@(-20));
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self).offset(-0.5);
        make.left.equalTo(@(5));
        make.right.equalTo(@(-5));
        make.height.equalTo(@(0.5));
    }];
}

- (void)switchAction:(UISwitch *)sender
{
    
    if (sender.on == YES) {
        
        [self saveCattleState:@"1"];
        [[LDShowHUB shareInstance]showSucc:@"开启"];
    }
    
    if (sender.on == NO) {
        
        [self saveCattleState:@"0"];
        [[LDShowHUB shareInstance]showErr:@"关闭"];
    }
}

- (void) saveCattleState:(NSString *)state
{

    DDCattleResume *cattleResume = [[DDCattleResume alloc]init];
 
    [cattleResume saveCattleResumeStateWithCattleUserID:[DDLoginUserManager userID] resumeShow:state SuccessBlock:^(id responseObject) {
                
    } failBlock:^(id responseObject, NSError *error) {
        
        [[LDShowHUB shareInstance]showErr:@"操作失败"];
    }];
    
}

//- (void)awakeFromNib {
//    [super awakeFromNib];
//    // Initialization code
//}

//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
//
//    // Configure the view for the selected state
//}

@end
